home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Tools / Win95 Secrets / SETUP.Z / PHYS16.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-19  |  5.2 KB  |  182 lines

  1. //==================================
  2. // PHYS - Matt Pietrek 1995
  3. // FILE: PHYS16.C
  4. //==================================
  5.  
  6. #include <windows.h>
  7. #include "descript.h"
  8.  
  9. BOOL FAR PASCAL __export DllEntryPoint (DWORD dwReason,
  10.                                WORD  hInst,
  11.                                WORD  wDS,
  12.                                WORD  wHeapSize,
  13.                                DWORD dwReserved1,
  14.                                WORD  wReserved2);
  15.  
  16. BOOL FAR PASCAL phystk_ThunkConnect16(    LPSTR pszDll16,
  17.                                         LPSTR pszDll32,
  18.                                         WORD  hInst,
  19.                                         DWORD dwReason);
  20.  
  21. int FAR PASCAL LibMain (HANDLE hInstance,
  22.                         WORD   wDataSeg,
  23.                         WORD   wHeapSize,
  24.                         LPSTR  lpszCmdLine)
  25. {
  26.     if (wHeapSize != 0)   // If DLL data seg is MOVEABLE
  27.         UnlockData (0);
  28.  
  29.     return (1);
  30. }
  31.  
  32. BOOL FAR PASCAL __export DllEntryPoint (DWORD dwReason,
  33.                                WORD  hInst,
  34.                                WORD  wDS,
  35.                                WORD  wHeapSize,
  36.                                DWORD dwReserved1,
  37.                                WORD  wReserved2)
  38. {
  39.     OutputDebugString("In 16bit DllEntryPoint: Calling phystk_ThunkConnect16\r\n");
  40.     if (!phystk_ThunkConnect16(    "PHYS16.DLL",
  41.                                 "PHYS32.DLL",
  42.                                 hInst,
  43.                                 dwReason))
  44.     {
  45.         OutputDebugString("In 16bit DllEntryPoint: phystk_ThunkConnect16 ret FALSE\r\n");
  46.         return FALSE;
  47.     }
  48.  
  49.     OutputDebugString("In 16bit DllEntryPoint: phystk_ThunkConnect16 ret TRUE\r\n");
  50.     return TRUE;
  51. }
  52.  
  53. static char MS_DOS_STR[] = "MS-DOS";
  54.  
  55. //
  56. // Return a far pointer to the LDT
  57. //
  58. unsigned short GetLDTAlias(void)
  59. {
  60.     unsigned short  LDT_alias;
  61.     unsigned short  (far * dpmiproc)(void);
  62.  
  63.     //
  64.     // Use INT 2Fh, fn. 168A to get the "DPMI extensions
  65.     // entry point" function pointer
  66.     //
  67.     _asm     mov     si, offset MS_DOS_STR   // DS:SI = "MS-DOS"
  68.     _asm     mov     ax, 168Ah
  69.     _asm     int     2Fh
  70.     _asm     cmp     al, 8Ah
  71.     _asm     je      extensions_not_found
  72.  
  73.     //
  74.     // The entry point is returned in ES:DI.  Save it
  75.     //
  76.     _asm     mov     word ptr [dpmiproc], di
  77.     _asm     mov     word ptr [dpmiproc+2], es
  78.  
  79.     //
  80.     // Call the extensions with AX == 0x100.  The LDT alias
  81.     // selector is return in AX.  Carry flag is set on failure.
  82.     //
  83.     _asm     mov     ax, 100h
  84.     LDT_alias = dpmiproc();
  85.     _asm     jc      extensions_not_found;
  86.  
  87.     return  LDT_alias;
  88.  
  89. extensions_not_found:   // We get here if something failed
  90.     return  0;
  91. }
  92.  
  93. WORD FAR PASCAL __loadds GetRing0Callgate( DWORD func_address,
  94.                                             unsigned cParams )
  95. {
  96.     CALLGATE_DESCRIPTOR far *callgate_desc;
  97.     CODE_SEG_DESCRIPTOR far *ring0_desc;
  98.     unsigned short ldt_alias;
  99.     unsigned short ring_0_alias;
  100.     unsigned short callgate_selector;
  101.  
  102.     if ( (ldt_alias = GetLDTAlias()) == 0 )
  103.         return 0;
  104.  
  105.     //
  106.     // Grab a selector from Windows to use as the CS at ring 0
  107.     //
  108.     if ( !(ring_0_alias = AllocSelector(0)) )
  109.         return 0;
  110.  
  111.     //
  112.     // Set up the fields in the descriptor to be a ring 0, flat model seg
  113.     //
  114.     ring0_desc = MAKELP( ldt_alias, ring_0_alias & 0xFFF8 );
  115.     ring0_desc->limit_0_15 = 0xFFFF;
  116.     ring0_desc->base_0_15 = 0;
  117.     ring0_desc->base_16_23 = 0;
  118.     ring0_desc->readable = 1;
  119.     ring0_desc->conforming = 0;
  120.     ring0_desc->code_data = 1;
  121.     ring0_desc->app_system = 1;
  122.     ring0_desc->dpl = 0;
  123.     ring0_desc->present = 1;
  124.     ring0_desc->limit_16_19 = 0xF;
  125.     ring0_desc->always_0 = 0;
  126.     ring0_desc->seg_16_32 = 1;
  127.     ring0_desc->granularity = 1;
  128.     ring0_desc->base_24_31 = 0;
  129.     
  130.     //
  131.     // Allocate the selector that'll be used for the call gate
  132.     //
  133.     if ( (callgate_selector= AllocSelector(0)) == 0 )
  134.     {
  135.         FreeSelector( ring_0_alias );
  136.         return 0;
  137.     }
  138.  
  139.     //
  140.     // Create a pointer to the call gate descriptor
  141.     //
  142.     callgate_desc = MAKELP( ldt_alias, callgate_selector & 0xFFF8 );
  143.  
  144.     //
  145.     // Fill in the fields of the call gate descriptor with the
  146.     // appropriate values for a 16 bit callgate.
  147.     //
  148.     callgate_desc->offset_0_15 = LOWORD( func_address );
  149.     callgate_desc->selector = ring_0_alias;
  150.     callgate_desc->param_count = cParams;
  151.     callgate_desc->some_bits = 0;
  152.     callgate_desc->type = 0xC;          // 386 call gate
  153.     callgate_desc->app_system = 0;      // A system descriptor
  154.     callgate_desc->dpl = 3;             // Ring 3 code can call
  155.     callgate_desc->present = 1;
  156.     callgate_desc->offset_16_31 = HIWORD(func_address);
  157.  
  158.     return callgate_selector;
  159. }
  160.  
  161. BOOL FAR PASCAL __loadds FreeRing0Callgate( WORD callgate )
  162. {
  163.     CALLGATE_DESCRIPTOR far *callgate_desc;
  164.     unsigned short ldt_alias;
  165.  
  166.     if ( (ldt_alias = GetLDTAlias()) == 0 )
  167.         return FALSE;
  168.  
  169.     //
  170.     // Create a pointer to the call gate descriptor
  171.     //
  172.     callgate_desc = MAKELP( ldt_alias, callgate & 0xFFF8 );
  173.  
  174.     //
  175.     // First, free the ring 0 alias selector stored in the LDT
  176.     // call gate descriptor, then free the call gate selector.
  177.     //
  178.     FreeSelector( callgate_desc->selector );
  179.     FreeSelector( callgate );
  180. }
  181.  
  182.